/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vzone.invest;

import java.util.ArrayList;
import vzone.tools.Date;
import java.util.UUID;
import vzone.bank.Bank;
import vzone.community.Community;
import vzone.structs.Billing;
import vzone.tools.SqlHelper;
import vzone.structs.Company;
import vzone.structs.InvestedApply;
import vzone.structs.OperatorCode;
import vzone.structs.ToInvestApply;

/**
 *
 * @author Kingpro
 */
public class Invest implements IInvest
{

    private static int companyType = 0;
    private Company company;
    private static final String SELECTCOMPANY = "SELECT * FROM [c_company] WHERE [companyId]=? AND [companyType]=?";
    private static final String INVESTEDAPPLY = "INSERT INTO [i_invested] VALUES(?,?,?,?,?,?,?)";
    private static final String TOINVESTAPPLY = "INSERT INTO [i_toInvest] VALUES(?,?,?,?,?,?,?)";

    public Invest(UUID companyID) throws Exception
    {

	company = new Company();

	ArrayList companysAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(), SELECTCOMPANY, new Object[]
		{
		    companyID,
		    companyType
		});
	if (companysAl.size() == 0)
	{
	    throw new Exception("无此公司");
	}
	Object[] resultObjects = (Object[]) companysAl.get(0);
	company.IdUuid = companyID;
	company.Owner = Community.getCommunity().GetPeopleInfo(UUID.fromString((String) resultObjects[2]));
	company.NameS = (String) resultObjects[1];
	company.StartD =Long.parseLong(resultObjects[3].toString());
	company.accountIdUuid = UUID.fromString((String) resultObjects[4]);
	company.pwd = (String) resultObjects[5];
	company.DescriptionS = (String) resultObjects[6];

    }

    @Override
    public OperatorCode ApplyForInvested(InvestedApply apply)
    {
	// return c.NOBILLING;
	//根据BalanceD,AccountIDUuid去银行查询该账户信用记录
	if (apply.BalanceD == 0)
	{
	    return OperatorCode.LESSHORNER;
	}
	else if (apply.AccountIDUuid == null)
	{
	    return OperatorCode.NOACCOUNT;
	}
	else
	{
	    try
	    {
		apply.ApplyIDUuid = UUID.randomUUID();
		apply.StartDate = new Date();
		SqlHelper.ExecuteNonQuery(SqlHelper.getConnect(), INVESTEDAPPLY, new Object[]
			{
			    apply.ApplyIDUuid.toString(),
			    apply.People.PeopleIdUuid.toString(),
			    apply.BalanceD,
			    apply.AccountIDUuid.toString(),
			    null,
			    apply.StartDate.getTime(),
			    apply.DescriptionS
			});
		return OperatorCode.SUCCESS;
		//将资金转入该账户

	    }
	    catch (Exception ex)
	    {
		return OperatorCode.OTHER_ERROR;
	    }
	}
    }

    @Override
    public OperatorCode ApplyToInvest(ToInvestApply apply)
    {

	if (apply.BalanceD == 0)
	{
	    return OperatorCode.LESSHORNER;
	}
	if (apply.AccountIDUuid == null)
	{
	    return OperatorCode.NOACCOUNT;
	}
	if (apply.BillingIDUuid == null)
	{
	    return OperatorCode.NOBILLING;
	}
	Billing billing = new Billing();
	billing.ToAccount = company.accountIdUuid;
	billing.BillingIDUuid = apply.BillingIDUuid;

	if (Bank.getBank().CheckInBilling(billing, company.pwd) != OperatorCode.SUCCESS)
	{
	    return OperatorCode.OTHER_ERROR;
	}

	try
	{
	    apply.ApplyIDUuid = UUID.randomUUID();
	    apply.StartDate = new Date();

	    SqlHelper.ExecuteNonQuery(SqlHelper.getConnect(), TOINVESTAPPLY, new Object[]
		    {
			apply.AccountIDUuid.toString(),
			apply.people.PeopleIdUuid.toString(),
			billing.Balance,
			billing.FromAccount.toString(),
			billing.BillingIDUuid.toString(),
			apply.StartDate.getTime(),
			null
		    });
	    return OperatorCode.SUCCESS;
	    //该用户是否将钱转过来了

	}
	catch (Exception ex)
	{
	    return OperatorCode.OTHER_ERROR;
	}
    }
}